package com.sec.android.app.myfiles.module.local.category;

import android.content.Context;
import android.graphics.Point;
import android.view.View;
import android.view.WindowManager;
import android.widget.GridLayout;
import android.widget.TextView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.fragment.home.HomeFragment;
import com.sec.android.app.myfiles.fragment.home.HomeFragmentItem;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.util.UiUtils;

public class CategoryHomeItem extends HomeFragmentItem {
    private TextView mCategoryHeader;
    protected GridLayout mGridLayout;

    CategoryHomeItem(Context context, HomeFragment fragment) {
        super(context, fragment);
    }

    @Override
    protected void setListItem() {
        HomeItemInfo item;
        HomeItemClickListener onclick;
        FileRecord record;

        View categoryView = mRootView.findViewById(R.id.home_category_grid);
        setViewVisibility(categoryView, View.VISIBLE);
        mCategoryHeader = (TextView) mRootView.findViewById(R.id.home_category_header);
        setViewVisibility(mCategoryHeader, View.VISIBLE);

        record = FileRecord.createFileRecord(StorageType.Category, AppConstants.StoragePath.CATEGORY_IMAGE);
        onclick = new HomeItemClickListener(record, "Category-Images");
        item = new HomeItemInfo(R.id.home_list_item_image_stub, R.id.home_list_item_image, R.drawable.my_files_ic_image,
                R.color.home_icon_image_color, R.color.home_icon_image_bg_color, R.string.images, onclick);
        mItemList.add(item);

        record = FileRecord.createFileRecord(StorageType.Category, AppConstants.StoragePath.CATEGORY_VIDEO);
        onclick = new HomeItemClickListener(record, "Category-Videos");
        item = new HomeItemInfo(R.id.home_list_item_video_stub, R.id.home_list_item_video, R.drawable.my_files_ic_video,
                R.color.home_icon_video_color, R.color.home_icon_video_bg_color, R.string.videos, onclick);
        mItemList.add(item);

        record = FileRecord.createFileRecord(StorageType.Category, AppConstants.StoragePath.CATEGORY_AUDIO);
        onclick = new HomeItemClickListener(record, "Category-Audio");
        item = new HomeItemInfo(R.id.home_list_item_music_stub, R.id.home_list_item_music, R.drawable.my_files_ic_music,
                R.color.home_icon_music_color, R.color.home_icon_music_bg_color, R.string.audio, onclick);
        mItemList.add(item);

        record = FileRecord.createFileRecord(StorageType.Category, AppConstants.StoragePath.CATEGORY_DOCUMENT);
        onclick = new HomeItemClickListener(record, "Category-Documents");
        item = new HomeItemInfo(R.id.home_list_item_document_stub, R.id.home_list_item_document, R.drawable.my_files_ic_document,
                R.color.home_icon_document_color, R.color.home_icon_document_bg_color, R.string.documents, onclick);
        mItemList.add(item);

        record = FileRecord.createFileRecord(StorageType.Category, AppConstants.StoragePath.CATEGORY_APK);
        onclick = new HomeItemClickListener(record, "APK Collector");
        item = new HomeItemInfo(R.id.home_list_item_apk_stub, R.id.home_list_item_apk, R.drawable.myfiles_ic_apk, R.color.home_icon_apk_color, R.color.home_icon_apk_bg_color, UiUtils.getInstallationFileString(), onclick);
        mItemList.add(item);

        updateCategoryItemLayout();
    }

    public void updateCategoryItemLayout() {
        int screenWidth;
        mGridLayout = (GridLayout) mRootView.findViewById(R.id.category_home_grid_layout);
        if (mGridLayout != null) {
            int paddingSide = (int) mContext.getResources().getDimension(R.dimen.home_category_view_padding_side);
            mGridLayout.setPadding(paddingSide, 0, paddingSide, (int) mContext.getResources().getDimension(R.dimen.category_grid_view_padding_bottom));
            screenWidth = getScreenWidth() - (2 * paddingSide);
            if (UiUtils.getScreenState(mContext) == AppConstants.LANDSCAPE && screenWidth >= (int) mContext.getResources().getDimension(R.dimen.multi_window_landscape_minimum_width)) {
                setCategoryItemLayout(screenWidth / 6);
                mGridLayout.setColumnCount(6);
                mGridLayout.setRowCount(1);
            } else {
                setCategoryItemLayout(screenWidth / 3);
                mGridLayout.setColumnCount(3);
                mGridLayout.setRowCount(2);
            }
        }
    }

    private int getScreenWidth() {
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        Point size = new Point();
        wm.getDefaultDisplay().getSize(size);
        return size.x;
    }

    public void setCategoryItemLayout(int width) {
        int itemId;
        itemId = R.id.home_list_item_image;
        setCategoryItemSize(itemId, width);
        itemId = R.id.home_list_item_apk;
        setCategoryItemSize(itemId, width);
        itemId = R.id.home_list_item_video;
        setCategoryItemSize(itemId, width);
        itemId = R.id.home_list_item_music;
        setCategoryItemSize(itemId, width);
        itemId = R.id.home_list_item_download_history;
        setCategoryItemSize(itemId, width);
        itemId = R.id.home_list_item_document;
        setCategoryItemSize(itemId, width);
    }

    private void setCategoryItemSize(int itemId, int width) {
        View stub = mRootView.findViewById(itemId);
        GridLayout.LayoutParams param = new GridLayout.LayoutParams();
        param.width = width;
        stub.setPadding(0, (int) mContext.getResources().getDimension(R.dimen.category_grid_item_padding_top), 0, 0);
        stub.setLayoutParams(param);
    }

    @Override
    protected void _onResume() {
        setContentDescription(mCategoryHeader, R.string.subtitle_category);
    }

    @Override
    protected void setItemEnabled(boolean enabled) {
        super.setItemEnabled(enabled);
        setChildItemEnabled(mCategoryHeader, enabled);
    }

    @Override
    protected void _onDestroy() {
    }

    @Override
    protected void _onPause() {
    }
}
